import 'package:jiffy/jiffy.dart';
import 'package:safetymonitor/app/components/custom_app_bar.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_file_upload_page.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_high_input_page.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_input_page.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/ext/widget_extension.dart';
import 'package:safetymonitor/app/core/utils/file_util.dart';
import 'package:safetymonitor/app/core/utils/interface.dart';
import 'package:safetymonitor/app/core/utils/user_manager.dart';

import 'controller.dart';

///整改中
class UnderRectificationStatePage extends StatelessWidget {
  UnderRectificationStatePage({super.key});

  final controller = Get.put(UnderRectificationStateController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.bg_color_grey,
      appBar: CustomAppBar(
        titleText: '整改中',
      ),
      body: GetBuilder(
        init: controller,
        builder: (_) {
          return SingleChildScrollView(
              child: UserManager.getUserCompanyId() != 100&&controller.preventiveRectificationGetEntity.rectificationUserIds!.split(',').contains(UserManager.getUserId().toString())//只有整改人才可以整改
                  ? Column(
                children: [
                  AppGaps.vGap10,
                  _getStateView(),
                  Obx(() {
                    return controller.currentIndex.value == 0
                        ? _getNormalView() // 基本信息
                        : _getExistenceView(); //
                  }),
                ],
              )
                  : _getNormalView() // 基本信息,111
          );
        },
      ),
    );
  }
  Widget _getNormalView() {
    return Column(
      children: [
        _titleBg(),
        Container(
          height: 40.w,
          width: double.infinity,
          color: const Color(0xFFF4F7FF),
          alignment: Alignment.centerLeft,
          padding: EdgeInsets.only(left: 40.w),
          child: Text(
            '隐患信息',
            style: TextStyle(
              fontSize: 16.sp,
            ),
          ),
        ),
        Container(
          height: 1.w,
          color: const Color(0xFFDEE5FD),
        ),
        Container(
          color: Colors.white,
          padding: EdgeInsets.only(
              left: 20.w, right: 20.w, top: 20.w, bottom: 20.w),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _rowData(
                  '隐患名称：',
                  controller
                      .preventiveRectificationGetEntity.preventiveName),
              AppGaps.vGap10,
              _rowData(
                  '责任单位：',
                  controller
                      .preventiveRectificationGetEntity.companyName),
              AppGaps.vGap10,
              _rowData('责任部门：', controller.preventiveRectificationGetEntity.deptNames),
              AppGaps.vGap10,
              _rowData('责任人：', controller.preventiveRectificationGetEntity.responsibleUserNames),
              AppGaps.vGap10,
              _rowData(
                  '发现时间：',
                  controller
                      .preventiveRectificationGetEntity.findTime!=0?Jiffy.parseFromMillisecondsSinceEpoch(controller
                      .preventiveRectificationGetEntity.findTime)
                      .format(pattern: 'yyyy-MM-dd HH:mm:ss'):''),
              AppGaps.vGap10,
              _rowData(
                  '整改期限：',
                  controller
                      .preventiveRectificationGetEntity
                      .rectificationDeadline!=0?Jiffy.parseFromMillisecondsSinceEpoch(controller
                      .preventiveRectificationGetEntity
                      .rectificationDeadline)
                      .format(pattern: 'yyyy-MM-dd HH:mm:ss'):''),
              AppGaps.vGap10,
              _rowData(
                  '整改人：',
                  controller.preventiveRectificationGetEntity
                      .rectificationUserNames),
              AppGaps.vGap10,
              _rowData('整改部门：', controller.preventiveRectificationGetEntity.rectificationDeptNames),
              AppGaps.vGap10,
              _rowData('隐患来源：',
                  controller.preventiveRectificationGetEntity.source),
              AppGaps.vGap10,
              _rowData(
                  '问题描述：',
                  controller
                      .preventiveRectificationGetEntity.problemDesc),
              AppGaps.vGap10,
              _rowData(
                  '整改要求：',
                  controller.preventiveRectificationGetEntity
                      .rectificationRequire),
              AppGaps.vGap12,

              // _getBottomView()
              CustomFileUploadPage(
                isRequired: false,
                isShowDivider: false,
                isEnabled: false,
                isChange: false,
                uploadedFiles:  controller.preventiveRectificationGetEntity.fileUrl.isNotEmpty?  controller.preventiveRectificationGetEntity.fileUrl.split(','):[],
                title: '附件：',
                onCallBack: (value) {},
              ),
            ],
          ),
        ),
        _titleBg(),
        Container(
          height: 40.w,
          width: double.infinity,
          color: const Color(0xFFF4F7FF),
          alignment: Alignment.centerLeft,
          padding: EdgeInsets.only(left: 40.w),
          child: Text(
            '整改信息',
            style: TextStyle(
              fontSize: 16.sp,
            ),
          ),
        ),
        Container(
          height: 1.w,
          color: const Color(0xFFDEE5FD),
        ),
        Column(
          children: controller
              .preventiveRectificationGetEntity.recordDOS
              .map((e) {
            return Container(
              color: Colors.white,
              padding: EdgeInsets.only(
                  left: 20.w, right: 20.w, top: 20.w, bottom: 20.w),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  _rowData('整改人：', e.rectificationPersonName),
                  AppGaps.vGap10,
                  _rowData('整改时间：',  e.rectificationTime!=0?Jiffy.parseFromMillisecondsSinceEpoch( e.rectificationTime).format(pattern: 'yyyy-MM-dd HH:mm:ss'):''),
                  AppGaps.vGap10,
                  _rowData('完成情况：','${e.rectificationProgress}%'),
                  AppGaps.vGap10,
                  _rowData('整改进度：', e.rectificationDesc ),
                  AppGaps.vGap10,
                  _rowData('整改措施：', e.rectificationActions),
                  AppGaps.vGap12,
                  // _getBottomView(),
                  CustomFileUploadPage(
                    isRequired: false,
                    isShowDivider: false,
                    isEnabled: false,
                    isChange: false,
                    uploadedFiles: e.rectificationFile.isNotEmpty? e.rectificationFile.split(','):[],
                    title: '附件：',
                    onCallBack: (value) {},
                  ),
                ],
              ),
            );
          }).toList(),
        ),
        ]
    );
  }

  Widget _rowData(String name, String? value,
      {Color valueColor = const Color(0xFF495061)}) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          name,
          style: TextStyle(
            color: const Color(0xFFA5A6B5),
            height: 1.2,
            fontWeight: FontWeight.w400,
            fontSize: 12.sp,
          ),
        ),
        Expanded(
            child: Text(
          value ?? '空',
          style: TextStyle(
            color: valueColor,
            height: 1.2,
            fontSize: 12.sp,
            fontWeight: FontWeight.w400,
          ),
        )),
      ],
    );
  }

  /// 标题背景分隔栏
  Widget _titleBg() {
    return Column(
      children: [
        AppGaps.vGap12,
        Container(
          height: 2.w,
          color: const Color(0xFF587DF5),
        ),
      ],
    );
  }

  Widget _getBottomView() {
    return Column(
      children: [
        Text(
          '拍照:',
          style: TextStyle(
            fontSize: 13.sp,
            color: const Color(0xffA5A6B5),
            overflow: TextOverflow.ellipsis,
            fontWeight: FontWeight.w400,
          ),
        ),
        AppGaps.vGap6,
        Wrap(
            spacing: 5.w,
            children: controller.dangerImages!.map<Widget>((item) {
              return Container(
                width: 74.w,
                height: 74.w,
                margin: EdgeInsets.only(bottom: 10.w),
                decoration: BoxDecoration(
                  color: const Color(0xffF7F7FB),
                  border: Border.all(
                    color: const Color(0xffEBEDF0),
                  ),
                  borderRadius: BorderRadius.circular(8.w),
                  image: DecorationImage(
                    image: NetworkImage(Interface.fileUrl + item),
                    fit: BoxFit.cover,
                  ),
                ),
              ).clickShowLarge(url: Interface.fileUrl + item);
            }).toList()),
        AppGaps.vGap12,
        Text(
          '视频:',
          style: TextStyle(
            fontSize: 13.sp,
            color: const Color(0xffA5A6B5),
            overflow: TextOverflow.ellipsis,
            fontWeight: FontWeight.w400,
          ),
        ),
        AppGaps.vGap6,
        Wrap(
            spacing: 5.w,
            children: controller.dangerVideo!.map<Widget>((item) {
              return Container(
                width: 160.w,
                height: 90.w,
                margin: const EdgeInsets.only(bottom: 10),
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  color: const Color(0xff060B15),
                  borderRadius: BorderRadius.circular(8),
                ),
                child: IconButton(
                  icon: const Icon(
                    Icons.play_circle,
                    color: Colors.white,
                    size: 30,
                  ),
                  onPressed: () {
                    FileUtils.openFile(Interface.fileUrl + item);
                  },
                ),
              );
            }).toList()),
      ],
    );
  }

  Widget _getExistenceView() {
    return Column(
      children: [
        _titleBg(),
        Container(
          color: Colors.white,
          padding:
          EdgeInsets.only(left: 20.w, right: 20.w, top: 20.w, bottom: 20.w),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              CustomFileUploadPage(
                isShowDivider: false,
                splitCommaInUrl: true,
                // uploadedFiles: [],
                title: '附件',
                onCallBack: (value) {
                  final String imgUrl = value.join(',');
                  controller.fileUrl = imgUrl;
                },
              ),
              AppGaps.vGap20,
              Row(
                children: [
                  Expanded(
                    child: CustomInputPage(
                      textInputType: TextInputType.number,
                      reg:r'^\d*$', //正则去除小数输入
                      title: '完成情况：',
                      text: controller.rectificationProgress,
                      onCallBack: (value) {
                        controller.rectificationProgress = value;
                      },
                    ),
                  ),
                  const Text('%'),
                ],
              ),
              CustomHighInputPage(
                  title: '整改进度：',
                  text: controller.rectificationDesc,
                  hintText: '请输入完成情况',
                  onCallBack: (value) {
                    controller.rectificationDesc = value;
                  }),
              CustomHighInputPage(
                  title: '整改措施：',
                  text: controller.rectificationActions,
                  hintText: '请输入整改措施',
                  onCallBack: (value) {
                    controller.rectificationActions = value;
                  }),
              Container(
                width: double.infinity,
                color: Colors.white,
                padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 5.w),
                child: ElevatedButton(
                    style: ElevatedButton.styleFrom(
                      backgroundColor: AppColors.blue_button,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(22.r),
                      ),
                    ),
                    onPressed: controller.submit,
                    child: Text(
                      '提交',
                      style: TextStyle(
                        fontSize: 14.sp,
                        color: Colors.white,
                        fontWeight: FontWeight.w400,
                      ),
                    )),
              )
            ],
          ),
        ),
      ],
    );
  }
  Widget _getStateView() {
    return Obx(() {
      return Container(
        padding: EdgeInsets.symmetric(horizontal: 45.w),
        height: 64.w,
        child: Stack(
          alignment: Alignment.center,
          children: [
            Positioned(
              left: 0,
              child: Stack(
                alignment: Alignment.center,
                children: [
                  SvgPicture.asset(
                    controller.currentIndex.value == 0
                        ? 'assets/svg/icon_normal.svg'
                        : 'assets/svg/icon_un_normal.svg',
                    width: 143.w,
                    height: 40.w,
                  ),
                  Text(
                    '基本信息',
                    style: TextStyle(
                      fontSize: 14.sp,
                      color: controller.currentIndex.value == 0
                          ? const Color(0xFFffffff)
                          : const Color(0xFF9FA5B0),
                    ),
                  )
                ],
              ).clickOnce(() {
                controller.selectTitle(0);
              }),
            ),
            Positioned(
              right: 0,
              child: Stack(
                alignment: Alignment.center,
                children: [
                  SvgPicture.asset(
                    controller.currentIndex.value == 1
                        ? 'assets/svg/icon_existence.svg'
                        : 'assets/svg/icon_un_existence.svg',
                    width: 143.w,
                    height: 40.w,
                  ),
                  Text(
                    '隐患整改',
                    style: TextStyle(
                      fontSize: 14.sp,
                      color: controller.currentIndex.value == 1
                          ? const Color(0xFFffffff)
                          : const Color(0xFF9FA5B0),
                    ),
                  )
                ],
              ).clickOnce(() {
                controller.selectTitle(1);
              }),
            ),
          ],
        ),
      );
    });
  }
}
